package com.yx.learn.service.impl;

import java.text.SimpleDateFormat;
import java.util.List;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.yx.learn.dao.CompanyDao;
import com.yx.learn.domain.Company;
import com.yx.learn.service.CompanyService;

@Service("companyService")
@Transactional
public class CompanySerivceImpl implements CompanyService {
	
	@Autowired
	@Qualifier("companyDao")
	private CompanyDao companyDao;

	public void saveCompany(Company company) throws Exception{
		// TODO Auto-generated method stub
		companyDao.saveCompany(company);
	}

	public void deleteCompany(Company company) throws Exception{
		// TODO Auto-generated method stub
		companyDao.deleteCompany(company);
	}

	public void updateCompany(Company company) throws Exception{
		// TODO Auto-generated method stub
		companyDao.updateCompany(company);
	}

	public Company findById(int id) throws Exception{
		// TODO Auto-generated method stub
		Company company = companyDao.findById(id);
		return company;
	}

	@Transactional(propagation = Propagation.NOT_SUPPORTED)
	public List<Company> findAll() throws Exception{
		// TODO Auto-generated method stub
		List<Company> companies = companyDao.findAllCompanies();
		return companies;
	}

	@Transactional(propagation = Propagation.NOT_SUPPORTED)
	public List<Company> find(String hql) throws Exception{
		// TODO Auto-generated method stub
		List<Company> compaines = companyDao.findCompanies(hql);
		return compaines;
	}

	@Transactional(propagation = Propagation.NOT_SUPPORTED)
	public String findAllToJson() throws Exception{
		// TODO Auto-generated method stub
		List<Company> companies = this.findAll();
		int total =  companies.size();
		JSONArray ja = new JSONArray();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		for(int i=0;i<total;i++){
			JSONObject jo =  new JSONObject();
			jo.put("id", companies.get(i).getId());
			jo.put("companyCode", companies.get(i).getCompanyCode());
			jo.put("companyName", companies.get(i).getCompanyName());
			jo.put("address", companies.get(i).getAddress());
			jo.put("telephone", companies.get(i).getTelephone());
			jo.put("email",companies.get(i).getEmail());
			jo.put("createTime",sdf.format(companies.get(i).getCreateTime()));
			jo.put("fax", companies.get(i).getFax());
			jo.put("memo",companies.get(i).getMemo());
			ja.add(jo);
		}
		String json = ja.toString();
		json = "{\"total\":" + total + ",\"rows\":" + json + "}";
		return json;
	}

	@Transactional(propagation = Propagation.NOT_SUPPORTED)
	public String companiesToCombobox() throws Exception{
		// TODO Auto-generated method stub
		List<Company> companies = this.findAll();
		JSONArray ja = new JSONArray();
		for(int i=0;i<companies.size();i++){
			JSONObject jo =  new JSONObject();
			jo.put("id", companies.get(i).getId());
			jo.put("companyName", companies.get(i).getCompanyName());
			ja.add(jo);
		}
		String json = ja.toString();		
		return json;
	}
	
	@Transactional(propagation = Propagation.NOT_SUPPORTED)
	public String findCompanies(int pageNumber, int pageSize)throws Exception{
		String hql = "From Company";
		List<Company> companies =  companyDao.findCompanies(hql, pageNumber, pageSize);
		int total =  companyDao.findAllCompanies().size();
		JSONArray ja = new JSONArray();
		for(int i=0;i<companies.size();i++){
			JSONObject jo =  new JSONObject();
			jo.put("id", companies.get(i).getId());
			jo.put("companyCode", companies.get(i).getCompanyCode());
			jo.put("companyName", companies.get(i).getCompanyName());
			jo.put("address", companies.get(i).getAddress());
			jo.put("tellphone", companies.get(i).getTelephone());
			jo.put("email",companies.get(i).getEmail());
			jo.put("createTime",companies.get(i).getCreateTime());
			jo.put("fax", companies.get(i).getFax());
			jo.put("memo",companies.get(i).getMemo());
			ja.add(jo);
		}
		String json = ja.toString();
		json = "{\"total\":" + total + ",\"rows\":" + json + "}";	
		return json;
	}

}

